package com.fhz.module_mine;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.fhz.core_lib.base.AppManager;
import com.fhz.core_lib.base.BaseApplication;
import com.fhz.core_lib.bus.LiveDataBus;
import com.fhz.core_lib.dao.database.UserDatabase;
import com.fhz.core_lib.entity.LoggedInUser;
import com.fhz.core_lib.manager.DialogManager;
import com.fhz.core_lib.router.RouterFragmentPath;
import com.fhz.core_lib.utils.ImageLoader;
import com.fhz.core_lib.utils.SPUtils;
import com.fhz.core_lib.widget.DialogView;
import com.fhz.module_mine.databinding.MineFragmentBinding;
import com.fhz.module_mine.fileutils.FileHelper;
import com.tbruyelle.rxpermissions2.RxPermissions;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;

import io.reactivex.functions.Consumer;

@Route(path = RouterFragmentPath.Mine.PAGE_MINE)
public class MineFragment extends Fragment implements View.OnClickListener {

    private static final String TAG = "MineFragment";
    private MineViewModel mViewModel;

    public MineFragmentBinding binding;


    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        mViewModel = new ViewModelProvider(this, new MineViewModelFactory()).get(MineViewModel.class);
        binding = DataBindingUtil.inflate(inflater,R.layout.mine_fragment, container, false);
        View root = binding.getRoot();

        EventBus.getDefault().register(this);

        binding.fragmentProfileImage.setOnClickListener(this);

        return root;

    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.fragment_profile_image) {
            MIneActivity.Launch(AppManager.getInstance().currentActivity());
        }
    }

    @Subscribe
    public void getHeadImg(String headImg){
        ImageLoader.load(AppManager.getInstance().currentActivity(),headImg, binding.fragmentProfileImage);
    }

    //一定登陆了
    @Override
    public void onResume() {
        super.onResume();
        //数据库取值,更新页面的信息
        UserDatabase.getInstance(AppManager.getInstance().currentActivity())
                .userDao()
                .loadUserByNickname(SPUtils.getInstance().getString(SPUtils.NICKNAME,""))
                .observe(this, new Observer<LoggedInUser>() {
                    @Override
                    public void onChanged(LoggedInUser loggedInUser) {
//                        ImageLoader.load(AppManager.getInstance().currentActivity(),loggedInUser.getHeadImage(), binding.fragmentProfileImage);
                    }
                });

//        LiveDataBus.get().with("headImg",String.class).observe(this, new Observer<String>() {
//            @Override
//            public void onChanged(String s) {
//                Log.d(TAG, "更新头像: "+s);
//                ImageLoader.load(AppManager.getInstance().currentActivity(),s, binding.fragmentProfileImage);
//            }
//        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
        EventBus.getDefault().unregister(this);

    }
}
